/** bcmi_frame job interface
 *
 *  bcmi_frame job interface
 *
 *  @author : huicong
 *  @date : 2008-10-30
 *
 */

#include "FrameJob.h"
#include <sstream>
#include <iostream>


namespace bcmi_frame
{

    FrameJob::FrameJob():
              _taskIdCount(0),
                  _myFrame(NULL)
    {
    }

    FrameJob::~FrameJob()
    {
        _myFrame = NULL;
    }

    VOID FrameJob::linkFrame(_IN ParallelFrame *_frame)
    {
        _myFrame = _frame;
    }

    ParallelFrame* FrameJob::getFrame()
    {
        return _myFrame;
    }

    CSTRING FrameJob::jobName() const
    {
        return "FrameJobInterface";
    }

    HRESULT FrameJob::taskRegister(IN STRING _regName, INOUT FrameTask *_task)
    {

#ifdef LOG_FRAME_JOB
        {
            std::stringstream _infoBuffer;
            _infoBuffer << "Task register : name = " << _regName 
                << " , address = " << _task << " , id = " << _taskIdCount;
            _myFrame->getLogRecorder()->log(
                H_OK,
                bcmi_frame::JOB,
                __FUNCSIG__,
                (CSTRING)_infoBuffer.str().c_str());
        }
#endif //#ifdef LOG_FRAME_JOB

        HRESULT hr = H_OK;

        _taskMap[_regName] = _task;

        if (_task->beRegistered() == FALSE)
        {
            _task->setTaskName(_regName);
            _task->linkJob(this);
            _task->setTaskId(_taskIdCount++);
            _taskList.push_back(_task);
        }
        return hr;
    }

    HRESULT FrameJob::registerInitializeTask(INOUT FrameTask *_task)
    {
        _task->setTaskType(bcmi_frame::INITIALIZE);
        return taskRegister(__FRAME_INI_TASK_NAME, _task);
    }

    HRESULT FrameJob::registerFinalizeTask(INOUT FrameTask *_task)
    {
        _task->setTaskType(bcmi_frame::FINALIZE);
        return taskRegister(__FRAME_FIN_TASK_NAME, _task);
    }

    VOID FrameJob::setDefaultInitializeTask()
    {
        registerInitializeTask(new DefaultNullTask());
    }

    VOID FrameJob::setDefaultFinalizeTask()
    {
        registerFinalizeTask(new DefaultNullTask());
    }

}; //namespace bcmi_frame
